﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace EmployeeCheckIn
{
    public partial class RegisterPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int i;
            if (!IsPostBack)
            {
                i = GetID();
                string idGen = string.Format("SMMMS{0:000}", i);
                hdf_EmpID.Value = i.ToString();
                txtEmp_code.Text = idGen;
                TXTempty();

            }
            
        }
        private void TXTempty()
        {
            txtFirstName.Text = "";
            txtLastname.Text = "";
            txtStartdate.Text = "";
            txtBirthDate.Text = "";
            txtAddress.Text = "";
            txtIdCard.Text = "";
            txtPostId.Text = "";
            txtTel.Text = "";
        }
        public DateTime ConvestToDate(string s)
        {
            int day=1;
            int mount=1;
            int years=1111;
            try
            {
                string[] stemp = s.Split('-');
                day = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                years = Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                return new DateTime(years, mount, day);
            }
            catch (Exception)
            {

                lbStdate.Visible = true;
                lbStdate.Text = "*รูปแบบ วว-ดด-ปปปป";
                lbStdate.Visible = true;
                lbStdate.Text = "*รูปแบบ วว-ดด-ปปปป";
            }

            return new DateTime(years, mount, day);
            
        }
        public bool CheckConvestToDate(string s)
        {
            int day;
            int mount;
            int years;
            bool resault = false;
            try
            {
                string[] stemp = s.Split('-');
                day = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                years = Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                return resault = true;
            }
            catch (Exception)
            {

                resault = false;
            }
            return resault;

        }
        protected void btRegister_Click(object sender, EventArgs e)
        {
            InSertData intsert = new InSertData();
            EmpEntity emp = new EmpEntity();
            bool result = false;
            emp.ID = Convert.ToInt32(hdf_EmpID.Value);
            emp.EmpId = txtEmp_code.Text;
            emp.Prefix = Convert.ToInt32(rblPrefix.SelectedValue);
            emp.FirstName = txtFirstName.Text;
            emp.LastName = txtLastname.Text;
            emp.Position = Convert.ToInt32(ddlPosition.SelectedValue);
            if (CheckConvestToDate(txtBirthDate.Text)==false)
            {
                lbWarnBirth.Visible = true;
                lbWarnBirth.Text = "*รูปแบบ วว-ดด-ปปปป";
                result = true;
            }
            if (CheckConvestToDate(txtStartdate.Text)==false)
            {
                lbStdate.Visible = true;
                lbStdate.Text = "*รูปแบบ วว-ดด-ปปปป";
                result = true;
            }                       
            DateTime dtmBirth = ConvestToDate(txtBirthDate.Text);
            DateTime dtmStart = ConvestToDate(txtStartdate.Text);
            emp.DateBirth = dtmBirth;
            emp.DateStart = dtmStart;
            emp.CardID = txtIdCard.Text;
            emp.Address = txtAddress.Text;
            emp.City = Convert.ToInt32( ddlProvince.SelectedValue);
            emp.PostID = txtPostId.Text;
            emp.Phone = txtTel.Text;           
            if (chkID(emp.ID) > 0)
            {
                lbEmpCode.Visible = true;
                lbEmpCode.Text = "รหัสซ้ำลองอีกครั้ง";
                result = true;
            }
            if (txtFirstName.Text == "")
            {
                lbWarnName.Visible = true;
                lbWarnName.Text = "*กรุณากรอกชื่อ";
                result = true;
            }
            if (txtLastname.Text == "")
            {
                 lbWarnLastname.Visible = true;
                 lbWarnLastname.Text = "*กรุณากรอกนามสกุล";
                 result = true;
            }
            if (checknumber(txtPostId.Text) == false && txtPostId.Text != "")
            {
                lbWarnPost.Visible = true;
                lbWarnPost.Text = "*กรุณากรอกข้อมูล เป็นตัวเลข";
                result = true;
            }
            if (result == true)
            {
                return;
            }
            intsert.Insertss(emp);
            
        }
        public bool checknumber(string s)
        {
            //bool result = false;
            //string Str = s.Trim();
            //double Num;
            //bool isNum = double.TryParse(Str, out Num);
            //if (isNum)
            //{ 
            //    result = true; 
            //}
            //return result;

            bool result = false;
            try
            {
                Convert.ToInt32(s);
                result = true;
            }
            catch (Exception)
            {
                result = false;

            }
            return result;
        }      
        private int GetID()
        {
            int id;
            string sqltext = @"SELECT ISNULL (MAX(EMP_ID),0)
                                from EMPLOYEE";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection("Server=NUEK-PC\\SA;Database=DB_CHECK_EMPLOYEE;User Id=sa;Password=1234qwer;");
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            try
            {
                sqlcomm.Connection.Open();
                id = (int)sqlcomm.ExecuteScalar() + 1;
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
                throw;
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();
            return id;
        }
        private int chkID(int memberId)
        {
            int idchk;
            string sqltext = @"SELECT ISNULL (MAX(EMP_ID),0)
                                from EMPLOYEE WHERE EMP_ID =" + memberId + "";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection("Server=NUEK-PC\\SA;Database=DB_CHECK_EMPLOYEE;User Id=sa;Password=1234qwer;");
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            try
            {
                sqlcomm.Connection.Open();
                idchk = (int)sqlcomm.ExecuteScalar();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
                throw;
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();
            return idchk;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            TXTempty();
            Response.Redirect("Default.aspx");
        }

    }
}